﻿using AutoMapper;
using MediatR;
using SmartProduction.Domain.yjh;
using SmartProduction.ErrorCode;
using SmartProduction.Infarstructrue.Interface;
using SmartProduction.Write.API.Application.Commands.WorkFlow;

namespace SmartProduction.Write.API.Application.CommandHandlers.WorkFlow
{
    /// <summary>
    /// 工作流修改命令处理类
    /// </summary>
    public class WorkFlowUpdateCommandHandler:IRequestHandler<WorkFlowUpdateCommand,APIResult<object>>
    {
        /// <summary>
        /// 工作流仓储
        /// </summary>
        private readonly IBaseRepository<Workflows> workRep;
        /// <summary>
        /// mapper映射
        /// </summary>
        private readonly IMapper mapper;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="workRep">工作流仓储</param>
        /// <param name="mapper">mapper映射</param>
        public WorkFlowUpdateCommandHandler(IBaseRepository<Workflows> workRep, IMapper mapper)
        {
            this.workRep = workRep;
            this.mapper = mapper;
        }

        public async Task<APIResult<object>> Handle(WorkFlowUpdateCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();

            var entity = mapper.Map<Workflows>(request);

            var r = workRep.UpdateAsync(entity).Result;

            if (r > 0)
            {
                LogModel logModel = new LogModel();
                logModel.CreateDate = DateTime.Now;
                logModel.CreatePerson = "YJH";
                logModel.LoginThing = "正在修改工作流";
                result.Code = APIEnums.修改成功;
                result.Msg = APIEnums.修改成功.ToString();
                result.data = r;
            }
            else
            {
                result.Code = APIEnums.修改失败;
                result.Msg = APIEnums.修改失败.ToString();
                result.data = r;
            }

            return await Task.FromResult(result);
        }
    }
}
